/*
 * @Description: 登录、获取用户信息、退出登录、清除token等
 * @Author: liushuang
 * @Date: 2022-03-26 11:09:28
 * @LastEditTime: 2022-12-06 10:42:13
 */
// eslint-disable-next-line no-unused-vars
// import { login, logout, fetchUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'
// import { md5 } from 'web-utils-super'
import router from '@/router'

const states = () => ({
  hasToken: false, // 是否有token
  accessToken: '', // token，每个请求头中携带
  userInfo: {}, // 用户登录信息
  roles: [], // 权限列表
  admin: false, // 是否是管理员
  userInfoDetail: {}, // 用户详细信息
  checkedCampus: {}, // 当前用户所选校区（只与地图展示有关）
  updateSchool: false // 是否更新学校数据
})

const getters = {
  accessToken: (state) => state.accessToken,
  userInfo: (state) => state.userInfo,
  admin: (state) => state.admin,
  roles: (state) => state.roles
}

const mutations = {
  /**
   * @desc: 设置accessToken
   * @param {*} state
   * @param {*} accessToken
   */
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken
    accessToken
      ? localStorage.setItem('accessToken', accessToken)
      : localStorage.removeItem('accessToken')
  },
  setHasToken(state, hasToken) {
    state.hasToken = hasToken
    hasToken
      ? localStorage.setItem('hasToken', hasToken)
      : localStorage.removeItem('hasToken')
  },
  /**
   * @desc: 保存用户信息
   * @param {*} state
   * @param {*} userInfo
   */
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
    userInfo && Object.keys(userInfo).length > 0
      ? localStorage.setItem('userInfo', JSON.stringify(userInfo))
      : localStorage.removeItem('userInfo')
  },
  setUserInfoDetail(state, userInfoDetail) {
    state.userInfoDetail = userInfoDetail
  },
  /**
   * @desc: 保存用户权限
   * @param {*} state
   * @param {*} roles
   */
  setRoles(state, roles) {
    state.roles = roles
  },
  setFull(state, admin) {
    state.admin = admin
  },
  saveCheckedCampus(state, data) {
    state.checkedCampus = data
  },
  saveUpdateSchool(state, status) {
    state.updateSchool = status
  }
}

const actions = {
  /**
   * @desc: 登录并存储token
   * @param {*} commit
   * @param {*} username
   * @param {*} password
   */
  async login({ commit }, { rootOrgId }) {
    // const { data } = await login({
    //   username,
    //   password: md5(password),
    //   rootOrgId
    // }).catch((e) => e)
    // if (!data) {
    //   commit('setHasToken', false)
    //   return false
    // }
    const data = {
      userId: '1',
      name: '超级管理员',
      username: 'admin'
    }
    localStorage.setItem('rootOrgId', rootOrgId)
    commit('setUserInfo', data)
    ElMessage.success('欢迎登录')
    commit('setHasToken', true)
    return true
  },
  /**
   * @desc: 获取用户信息
   * @param {*} { commit, dispatch, state }
   */
  async getUserInfo({ commit }) {
    // const { data } = await fetchUserInfo().catch((err) => err)
    // if (!data) {
    //   dispatch('resetAll')
    //   return false
    // }
    const data = {
      userId: '1',
      name: '超级管理员',
      username: 'admin'
    }
    commit('setFull', data.username === 'admin')
    commit('setRoles', data.accessPermissions)
    commit('setUserInfoDetail', data)
    return true
  },
  /**
   * @desc: 退出登录
   * @param {*} commit
   */
  async logout({ dispatch }) {
    const rootOrgId = localStorage.getItem('rootOrgId')
    if (rootOrgId) {
      // const { data } = await logout({ rootOrgId })
      // if (!data) {
      //   dispatch('resetAll')
      // }
      dispatch('resetAll')
      localStorage.removeItem('rootOrgId')
    } else {
      dispatch('resetAll')
    }

    router.push('/login')
  },
  resetAll({ commit }) {
    commit('setAccessToken', '')
    commit('setFull', false)
    commit('setRoles', [])
    commit('setUserInfo', {})
    commit('setHasToken', false)
    window.location.reload()
  }
}

export default {
  state: states,
  getters,
  mutations,
  actions
}
